// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import com.alibaba.fastjson.JSONObject;
import com.liuxinlong.enums.RedisKeyEnum;
import com.liuxinlong.modules.common.entity.UserHeaderDto;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * redis工具类
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022年1月20日
 */
@Component
public class RedisUtils {
    @Resource
    public StringRedisTemplate stringRedisTemplate;

    public String getKey(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    public void setKey(String key, Object value) {
        stringRedisTemplate.opsForValue().set(key, value.toString());
    }

    public Long getExpire(String key) {
        return stringRedisTemplate.getExpire(key, TimeUnit.SECONDS);
    }

    public void setExpire(String key, long timeOut, TimeUnit timeType) {
        stringRedisTemplate.expire(key, timeOut, timeType);
    }

    public Boolean deleteKey(String key) {
        return stringRedisTemplate.delete(key);
    }

    public void setUserCache(String user, String token) {
        stringRedisTemplate.opsForValue().set(RedisKeyEnum.USER_LOGIN_CACHE.getKey() + user, token, 36000, TimeUnit.SECONDS);
    }

    public String getUserCache(String user) {
        return stringRedisTemplate.opsForValue().get(RedisKeyEnum.USER_LOGIN_CACHE.getKey() + user);
    }

    public void deleteUserCache(String user) {
        stringRedisTemplate.delete(RedisKeyEnum.USER_LOGIN_CACHE.getKey() + user);
    }

    public void setUserStatus(String user, int status) {
        stringRedisTemplate.opsForValue().set(RedisKeyEnum.USER_LOGIN_STATUS.getKey() + user, String.valueOf(status));
    }

    public String getUserStatus(String user) {
        return stringRedisTemplate.opsForValue().get(RedisKeyEnum.USER_LOGIN_STATUS.getKey() + user);
    }

    public void setUserHeaderCache(String user, UserHeaderDto userInfo) {
        stringRedisTemplate.opsForValue().set(RedisKeyEnum.USER_HEADER_INFO_CACHE.getKey() + user, JSONObject.toJSONString(userInfo));
    }

    public UserHeaderDto getUserHeaderCache(String user) {
        String valueStr = stringRedisTemplate.opsForValue().get(RedisKeyEnum.USER_HEADER_INFO_CACHE.getKey() + user);
        return JSONObject.parseObject(valueStr, UserHeaderDto.class);
    }

    public void setUserPointCache(String user, Map<String,Object> userInfo) {
        stringRedisTemplate.opsForValue().set(RedisKeyEnum.USER_POINT_INFO_CACHE.getKey() + user, JSONObject.toJSONString(userInfo));
    }

    public Map<String,Object> getUserPointCache(String user) {
        String valueStr = stringRedisTemplate.opsForValue().get(RedisKeyEnum.USER_POINT_INFO_CACHE.getKey() + user);
        return JSONObject.parseObject(valueStr, HashMap.class);
    }


    public void setUserInfoCache(String user, Map<String,Object> userInfo) {
        stringRedisTemplate.opsForValue().set(RedisKeyEnum.USER_BASE_INFO_CACHE.getKey() + user, JSONObject.toJSONString(userInfo));
    }

    public Map<String,Object> getUserInfoCache(String user) {
        String valueStr = stringRedisTemplate.opsForValue().get(RedisKeyEnum.USER_BASE_INFO_CACHE.getKey() + user);
        return JSONObject.parseObject(valueStr, HashMap.class);
    }

    public void setForumCache(Map<String, Object> statisticsMap) {
        stringRedisTemplate.opsForHash().putAll(RedisKeyEnum.FORUM_STATISTICS_CACHE.getKey(), statisticsMap);
    }

    public Map<Object, Object> getForumCache() {
        return stringRedisTemplate.opsForHash().entries(RedisKeyEnum.FORUM_STATISTICS_CACHE.getKey());
    }

    public void setPointsRulesCache(Map<String, Object> rulesMap) {
        stringRedisTemplate.opsForHash().putAll(RedisKeyEnum.POINTS_RULES_CACHE.getKey(), rulesMap);
    }

    public Map<Object, Object> getPointsRulesCache() {
        return stringRedisTemplate.opsForHash().entries(RedisKeyEnum.POINTS_RULES_CACHE.getKey());
    }


    /**
     * 获取全部key数据
     *
     * @return 全部key列表
     */
    public Set<String> getAllKeys() {
        Set<String> keys = stringRedisTemplate.keys("*");
        return keys;
    }

    public int getAllKeysCount() {
        Set<String> keys = stringRedisTemplate.keys("*");
        return keys.size();
    }

    /**
     * 清空全部缓存
     */
    public void clearAllCache() {
        Set<String> keys = stringRedisTemplate.keys("*");
        if (ObjectUtils.isEmpty(keys)) {
            return;
        }
        stringRedisTemplate.delete(keys);
    }

    /**
     * 获取key组下key列表
     *
     * @param key key组
     * @return key组下key列表
     */
    public Set<String> getKeyGroupCache(String key) {
        Set<String> keys = stringRedisTemplate.keys(key + "*");
       return keys;
    }

    /**
     * 根据key分组清空数据
     *
     * @param key key分组
     */
    public void clearKeyGroupCache(String key) {
        Set<String> keys = stringRedisTemplate.keys(key + "*");
        if (ObjectUtils.isEmpty(keys)) {
            return;
        }
        stringRedisTemplate.delete(keys);
    }

    /**
     * 根据key获取value
     *
     * @param key key
     * @return value数据
     */
    public String getKeyValue(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * 根据key删除数据
     *
     * @param key key
     */
    public void clearKeyCache(String key) {
        String value = stringRedisTemplate.opsForValue().get(key);
        if (ObjectUtils.isEmpty(value)) {
            return;
        }
        stringRedisTemplate.delete(key);
    }

}
